package com.atguigu.common.result;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * className: ResponseUtil
 * description:
 * date: 2023/4/17 0017-22:55
 * <p>
 * project: guigu-oa-parent
 * package: com.atguigu.common.result
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
public final class ResponseUtil {

    /**
     * response
     *
     * @param response {@link HttpServletResponse}
     * @param r        {@link Result<T>}
     */
    public static <T> void out(HttpServletResponse response, Result<T> r) {
        ObjectMapper mapper = new ObjectMapper();
        response.setStatus(HttpStatus.OK.value());
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setCharacterEncoding("utf-8");
        try {
            mapper.writeValue(response.getWriter(), r);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
